local mingcha = fk.CreateSkill {
  name = "1v1_ol__mingcha",
}

Fk:loadTranslationTable{
  ["1v1_ol__mingcha"] = "明察",
  [":1v1_ol__mingcha"] = "摸牌阶段开始时，你亮出牌堆顶三张牌，然后你可以放弃摸牌并获得其中点数不大于8的牌，" ..
  "若你以此法获得了牌，你可以选择一名有牌的其他角色，若其有手牌，随机获得其一张手牌，否则随机获得其一张装备牌。",

  ["#1v1_ol__mingcha-get"] = "明察：是否放弃摸牌，获得其中点数不大于8的牌？",
  ["#1v1_ol__mingcha-choose"] = "明察：你可以选择一名角色，随机获得其一张手牌或装备牌",

  ["$1v1_ol__mingcha1"] = "明主可以理夺，怎可以情求之？",
  ["$1v1_ol__mingcha2"] = "祸见于此，何免之有？",
}

mingcha:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(mingcha.name) and player.phase == Player.Draw
  end,
  on_use = function(self, event, target, player, data)
    ---@type string
    local skillName = mingcha.name
    local room = player.room
    local cards = room:getNCards(3)
    room:moveCards({
      ids = cards,
      toArea = Card.Processing,
      moveReason = fk.ReasonJustMove,
      skillName = skillName,
      proposer = player,
    })

    local choice = room:askToViewCardsAndChoice(player, {
      cards = cards,
      choices = { "OK", "Cancel" },
      skill_name = skillName,
      prompt = "#1v1_ol__mingcha-get"
    })
    if choice == "OK" then
      data.phase_end = true
      local to_get = {}
      for i = 3, 1, -1 do
        if Fk:getCardById(cards[i]).number < 9 then
          table.insert(to_get, cards[i])
          table.remove(cards, i)
        end
      end

      if #to_get > 0 then
        room:obtainCard(player, to_get, true, fk.ReasonPrey, player, skillName)

        if player:isAlive() then
          local targets = table.filter(room:getOtherPlayers(player, false), function(p) return not p:isNude() end)
          if #targets > 0 then
            local to = room:askToChoosePlayers(
              player,
              {
                targets = targets,
                min_num = 1,
                max_num = 1,
                prompt = "#1v1_ol__mingcha-choose",
                skill_name = skillName,
              }
            )
            if #to > 0 then
              local card = 0
              if #to[1]:getCardIds(Player.Hand) > 0 then
                card = table.random(to[1]:getCardIds(Player.Hand))
              else
                card = table.random(to[1]:getCardIds(Player.Equip))
              end
              room:obtainCard(player, card, false, fk.ReasonPrey, player, skillName)
            end
          end
        end
      end
    end
    if #cards > 0 then
      room:moveCards({
        ids = cards,
        fromArea = Card.Processing,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonJustMove,
        skillName = skillName,
      })
    end
  end,
})

return mingcha
